<template>
	<view class="content">
		<view class="uni-panel-c">
			<view class="uni-panel-bottom">
				<view class="uni-panel-item">
					<image class="uni-image" src="/static/img/back.png" mode="widthFix" @click="clickBack"></image>
					<view class="uni-panel-left">
						<text class="uni-text-item">你好，{{accountName}}同学，请选择要{{title}}的词汇包</text>
					</view>
				</view>
				<view class="uni-panel">
					<view class="uni-panel-book" v-for="(item, index) in listBook" :key="index" @click="clickBtnItem(item)">
						<view class="uni-text-word" >{{item.name}}</view>
						<view class="uni-book-bottom">
							<image class="uni-word-image" :src="apiUrl+item.image" mode="widthFix" ></image>
							<view class="uni-book-bottom-right">
								<text class="uni-text-word-right" >已学习：{{item.studyCount}}</text>
								<text class="uni-text-word-right" >未学习：{{item.wordsCount-item.studyCount}}</text>
								<text class="uni-text-word-right" >已完成：{{item.studyCount}} / {{item.wordsCount}}</text>
								<progress class="progress-box" :percent="item.wordsCount>0?(item.studyCount*100/item.wordsCount):0" activeColor="#10FFAE" backgroundColor="#aaa"/>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { getUserBookList } from '@/api/api.js';
	import store from "../../store";
	import { HTTP_REQUEST_URL } from '@/config/app.js';
	export default {
		mounted() {
		        // #ifdef APP-PLUS
		        plus.screen.lockOrientation('landscape');
		        // #endif
		    },
		data() {
			return {
				userId: 0,
				accountName: '',
				user_type: 0,
				listBook: [],
				toPage: '',
				title: '',
				apiUrl: HTTP_REQUEST_URL,
			}
		},
		onLoad(options) {
			this.toPage = options.toPage
			if('newword' == this.toPage){
				this.title = '复习生词'
			}else if('exam' == this.toPage){
				this.title = '进行测试'
			}else{
				this.title = '进行学习'
			}
		},
		onShow(){
			const userInfo = store.state.app.userInfo;
			if( userInfo){
				this.accountName = userInfo.nickname;
				this.userId = userInfo.id;
				this.user_type = userInfo.user_type;
			}
			let student = uni.getStorageSync('student_info');
			if( student){
				this.accountName = student.nickname;
				this.userId = student.id;
			}
				
			let that = this;
			let params = {user_id: this.userId};
			getUserBookList(params).then(res => {
				let ret = [];
				if( 0 == res.code && res.data ){
					for( let item of res.data.rows){
						ret.push(item);
					}
				}
				ret.sort((a,b)=> a.name.localeCompare(b.name));
				that.listBook = ret;
			}).catch(error => {
				// 处理异常
				console.error('发生错误:', error);
			});
		},
		methods: {
			clickBack(){
				uni.navigateBack();
			},
			clickBtnItem(item){
				if('newword' == this.toPage){
					let toParams = {user_id: this.userId, book_id: item.id, isNewWord:true}
					let params = Object.keys(toParams)
						.map(key => `${key}=${encodeURIComponent(toParams[key])}`)
						.join('&');
					uni.navigateTo({
						url: '/pages/studyex/index?' + params
					});
				}else if('exam' == this.toPage){
					let toParams = {book_id: item.id,
									isCheck: true}
					let params = Object.keys(toParams)
						.map(key => `${key}=${encodeURIComponent(toParams[key])}`)
						.join('&');
					if( 2 <= this.user_type){
						uni.navigateTo({
							url: '/pages/studyex/index?' + params
						});
					}else{
						uni.navigateTo({
							url: '/pages/exam/index?' + params
						});
					}
				}else{
					let params = Object.keys(item)
						.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(item[key])}`)
						.join('&');
					uni.navigateTo({
						url: '/pages/selectword/index?' + params
					});
				}
			}
		}
	}
</script>

<style>
	page {
		height: 100%;
	}
	.content {
		width: 100%;
		height: 100%;
		background-image: url('../../static/img/bg.jpg');
		background-size: 100% 100%;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.uni-panel-c {
		width: 90%;
		height: 100%;
		display: flex;
		flex-direction: column;
	}
	
	.uni-panel-top {
		width: 100%;
		height: 20%;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
	}
	.uni-image {
		width: 6%;
	}
	.uni-text-logo{
		color: black;
		font-size: 10vmin;
		margin-left: 2%;
		text-align: center;
		font-weight: bold;
		letter-spacing: 1rpx;
		font-family: "楷体";
	}
	
	.uni-panel-bottom-bk {
		position: absolute;
		left: 5%;
		top: 20%;
		width: 90%;
		height: 80%;
		background-color: white;
		opacity: 0.3;
		border-radius: 40rpx;
	}
	.uni-panel-bottom {
		margin-top: 8%;
		width: 100%;
		height: 80%;
		display: flex;
		flex-direction: column;
		align-items: center;
		z-index: 1;
	}
	.uni-panel-item {
		width: 96%;
		height: 15%;
		display: flex;
		flex-direction: row;
		align-items: center;
	}
	.uni-panel-left {
		display: flex;
		flex-direction: column;
		margin-left: 2%;
	}
	
	.uni-text-item{
		font-size: 5vmin;
		color: white;
		font-weight: bold;
		letter-spacing: 1rpx;
		text-shadow:
		    -1px -1px 0 #000,
		    1px -1px 0 #000,
		    -1px 1px 0 #000,
		    1px 1px 0 #000; /* 调整偏移量和颜色来设置描边宽度和颜色 */
	}
	
	.uni-panel {
		width:100%;
		height: 85%;
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
		overflow: auto;
	}
	
	.uni-panel-book {
		background-image: url('../../static/img/package.png');
		background-size: 100% 100%;
		height: 40%;
		width: 24%;
		margin-left: 1%;
		margin-top: 1%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		border-radius: 10rpx;
	}
	
	.uni-word-image {
		width: 20%;
		height: 80%;
	}
	
	.uni-text-word{
		font-size: 3vmin;
		color: black;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.uni-book-bottom{
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		margin-top: 3%;
		width: 80%;
	}
	
	.uni-book-bottom-right{
		display: flex;
		flex-direction: column;
		width: 65%;
		margin-left: 5%;
	}
	
	.uni-text-word-right{
		font-size: 2vmin;
		color: black;
	}
	
	.progress-box{
		width: 90%;
	}
</style>
